home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / drivers.doc < prev    next >
Text File  |  1996-01-10  |  40KB  |  951 lines

  1.    Copyright (C) 1989, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17.  
  18. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  19.  
  20. This file, drivers.doc, describes the interface between Ghostscript and
  21. device drivers.
  22.  
  23. For an overview of Ghostscript and a list of the documentation files, see
  24. README.
  25.  
  26. ********
  27. ******** Adding a driver ********
  28. ********
  29.  
  30. To add a driver to Ghostscript, all you need to do is edit devs.mak in
  31. two places.  The first is the list of devices, in the section headed
  32.  
  33. # -------------------------------- Catalog ------------------------------- #
  34.  
  35. Pick a name for your device, say smurf, and add smurf to the list.
  36. (Device names must be 1 to 8 characters, consisting of only letters,
  37. digits, and underscores, of which the first character must be a letter.
  38. Case is significant: all current device names are lower case.)
  39. The second is the section headed
  40.  
  41. # ---------------------------- Device drivers ---------------------------- #
  42.  
  43. Suppose the files containing the smurf driver are called joe and fred.
  44. Then you should add the following lines:
  45.  
  46. # ------ The SMURF device ------ #
  47.  
  48. smurf_=joe.$(OBJ) fred.$(OBJ)
  49. smurf.dev: $(smurf_)
  50.     $(SHP)gssetdev smurf $(smurf_)
  51.  
  52. joe.$(OBJ): joe.c ...and whatever it depends on
  53.  
  54. fred.$(OBJ): fred.c ...and whatever it depends on
  55.  
  56. If the smurf driver also needs special libraries, e.g., a library named
  57. gorf, then the gssetdev line should look like
  58.     $(SHP)gssetdev smurf $(smurf_)
  59.     $(SHP)gsaddmod smurf -lib gorf
  60.  
  61. ********
  62. ******** Keeping things simple
  63. ********
  64.  
  65. If you want to add a simple device (specifically, a black-and-white
  66. printer), you probably don't need to read the rest of this document; just
  67. use the code in an existing driver as a guide.  The Epson and BubbleJet
  68. drivers (gdevepsn.c and gdevbj10.c) are good models for dot-matrix printers,
  69. which require presenting the data for many scan lines at once; the
  70. DeskJet/LaserJet drivers (gdevdjet.c) are good models for laser printers,
  71. which take a single scan line at a time but support data compression.  For
  72. color printers, the DeskJet 500C driver (gdevcdj.c) may be a good place to
  73. start, although it is large and complex.  gdevcdj.c is also a good example
  74. of a device that has additional settable attributes (in this case, different
  75. output quality modes that aren't just a matter of resolution).
  76.  
  77. On the other hand, if you're writing a driver for some more esoteric
  78. device, you probably do need at least some of the information in the rest
  79. of this document.  It might be a good idea for you to read it in
  80. conjunction with one of the existing drivers.
  81.  
  82. ********
  83. ******** Driver structure ********
  84. ********
  85.  
  86. A device is represented by a structure divided into three parts:
  87.  
  88.     - procedures that are shared by all instances of each device;
  89.  
  90.     - parameters that are present in all devices but may be different
  91.       for each device or instance; and
  92.  
  93.     - device-specific parameters that may be different for each instance.
  94.  
  95. Normally, the procedure structure is defined and initialized at compile
  96. time.  A prototype of the parameter structure (including both generic and
  97. device-specific parameters) is defined and initialized at compile time,
  98. but is copied and filled in when an instance of the device is created.
  99.  
  100. The gx_device_common macro defines the common structure elements, with the
  101. intent that devices define and export a structure along the following
  102. lines:
  103.  
  104.     typedef struct smurf_device_s {
  105.         gx_device_common;
  106.         ... device-specific parameters ...
  107.     } smurf_device;
  108.     smurf_device gs_smurf_device = {
  109.         sizeof(smurf_device),        /* params_size */
  110.         0,                /* static_procs, obsolete */
  111.         ... generic parameter values ...
  112.         { ... procedures ... },        /* std_procs */
  113.         ... device-specific parameter values ...
  114.     };
  115.  
  116. The device structure instance *must* have the name gs_smurf_device, where
  117. smurf is the device name used in devs.mak.
  118.  
  119. All the device procedures are called with the device as the first
  120. argument.  Since each device type is actually a different structure type,
  121. the device procedures must be declared as taking a gx_device * as their
  122. first argument, and must cast it to smurf_device * internally.  For
  123. example, in the code for the "memory" device, the first argument to all
  124. routines is called dev, but the routines actually use md to reference
  125. elements of the full structure, by virtue of the definition
  126.  
  127.     #define md ((gx_device_memory *)dev)
  128.  
  129. (This is a cheap version of "object-oriented" programming: in C++, for
  130. example, the cast would be unnecessary, and in fact the procedure table
  131. would be constructed by the compiler.)
  132.  
  133. Structure definition
  134. --------------------
  135.  
  136. This essentially duplicates the first part of the structure definition in
  137. gxdevice.h.
  138.  
  139. typedef struct gx_device_s {
  140.     int params_size;        /* size of this structure */
  141.     gx_device_procs *static_procs;    /* (obsolete) */
  142.     const char *dname;        /* the device name */
  143.     int width;            /* width in pixels */
  144.     int height;            /* height in pixels */
  145.     ...
  146.     gx_device_color_info color_info;    /* color information */
  147.     ...
  148.     int is_open;            /* true if device has been opened */
  149. } gx_device;
  150.  
  151. The name in the structure should be the same as the name in devs.mak.
  152.  
  153. gx_device_common is a macro consisting of just the element definitions.
  154.  
  155. For sophisticated developers only
  156. ---------------------------------
  157.  
  158. If for any reason you need to change the definition of the basic device
  159. structure, or add procedures, you must change the following places:
  160.  
  161.     - This document and NEWS (if you want to keep the
  162.         documentation up to date).
  163.     - The definition of gx_device_common and/or the procedures
  164.         in gxdevice.h.
  165.     - Possibly, the default forwarding procedures in gxdevice.h
  166.         and gsdevice.c.
  167.     - The device procedure record completion routines in gsdevice.c.
  168.     - The following devices that must have complete (non-defaulted)
  169.         procedure vectors:
  170.         - The null device in gsdevice.c.
  171.         - The command list "device" in gxclist.c.  This is
  172.             not an actual device; it only defines procedures.
  173.         - The "memory" devices in gdevmem.h and gdevm*.c.
  174.     - The clip list accumulation "device" in gxacpath.c.
  175.     - The clipping "devices" in gxcpath.c and gxclip2.c.
  176.     - The Pattern accumulation "device" in gxpcmap.c.
  177.     - The hit detection "device" in zupath.c.
  178.     - The generic printer device macros in gdevprn.h.
  179.     - The generic printer device code in gdevprn.c.
  180.     - All the real devices in the standard Ghostscript distribution,
  181.         as listed in devs.mak.  (All of them are supposed to use
  182.         the macros in gxdevice.h or gdevprn.h to initialize their
  183.         state, so you may not have to edit the source code for
  184.         them.)
  185.     - Any other drivers you have that aren't part of the standard
  186.         Ghostscript distribution.
  187.  
  188. You may also have to change the code for gx_default_get_params and/or
  189. gx_default_put_params (in gsdparam.c).
  190.  
  191. Note that if all you are doing is adding optional procedures, you do NOT
  192. have to modify any of the real device drivers listed in devs.mak;
  193. Ghostscript will substitute the default procedures properly.
  194.  
  195. ********
  196. ******** Types and coordinates ********
  197. ********
  198.  
  199. Coordinate system
  200. -----------------
  201.  
  202. Since each driver specifies the initial transformation from user to device
  203. coordinates, the driver can use any coordinate system it wants, as long as
  204. a device coordinate will fit in an int.  (This is only an issue on MS-DOS
  205. systems, where ints are only 16 bits.  User coordinates are represented as
  206. floats.)  Typically the coordinate system will have (0,0) in the upper
  207. left corner, with X increasing to the right and Y increasing toward the
  208. bottom.  This happens to be the coordinate system that all the currently
  209. supported devices use.  However, there is supposed to be nothing in the
  210. rest of Ghostscript that assumes this.
  211.  
  212. Drivers must check (and, if necessary, clip) the coordinate parameters
  213. given to them: they should not assume the coordinates will be in bounds.
  214. The fit_fill and fit_copy macros in gxdevice.h are very helpful in doing
  215. this.
  216.  
  217. Color definition
  218. ----------------
  219.  
  220. Ghostscript represents colors internally as RGB or CMYK values.  In
  221. communicating with devices, however, it assumes that each device has a
  222. palette of colors identified by integers (to be precise, elements of type
  223. gx_color_index).  Drivers may provide a uniformly spaced gray ramp or
  224. color cube for halftoning, or they may do their own color approximation,
  225. or both.
  226.  
  227. The color_info member of the device structure defines the color and
  228. gray-scale capabilities of the device.  Its type is defined as follows:
  229.  
  230. typedef struct gx_device_color_info_s {
  231.     int num_components;        /* 1 = gray only, 3 = RGB, */
  232.                     /* 4 = CMYK */
  233.     int depth;            /* # of bits per pixel */
  234.     gx_color_value max_gray;    /* # of distinct gray levels -1 */
  235.     gx_color_value max_rgb;        /* # of distinct color levels -1 */
  236.                     /* (only relevant if num_comp. > 1) */
  237.     gx_color_value dither_gray;    /* size of gray ramp for halftoning */
  238.     gx_color_value dither_rgb;    /* size of color cube ditto */
  239.                     /* (only relevant if num_comp. > 1) */
  240. } gx_device_color_info;
  241.  
  242. The following macros (in gxdevice.h) provide convenient shorthands for
  243. initializing this structure for ordinary black-and-white or color devices:
  244.  
  245. #define dci_black_and_white ...
  246. #define dci_color(depth,maxv,dither) ...
  247.  
  248. The idea is that a device has a certain number of gray levels (max_gray +1)
  249. and a certain number of colors (max_rgb +1) that it can produce directly.
  250. When Ghostscript wants to render a given RGB or CMYK color as a device
  251. color, it first tests whether the color is a gray level.  (If
  252. num_components is 1, it converts all colors to gray levels.)  If so:
  253.  
  254.     - If max_gray is large (>= 31), Ghostscript asks the device to
  255. approximate the gray level directly.  If the device returns a valid
  256. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  257. the device can represent dither_gray distinct gray levels, equally spaced
  258. along the diagonal of the color cube, and uses the two nearest ones to the
  259. desired color for halftoning.
  260.  
  261. If the color is not a gray level:
  262.  
  263.     - If max_rgb is large (>= 31), Ghostscript asks the device to
  264. approximate the color directly.  If the device returns a valid
  265. gx_color_index, Ghostscript uses it.  Otherwise, Ghostscript assumes that
  266. the device can represent dither_rgb * dither_rgb * dither_rgb distinct
  267. colors, equally spaced throughout the color cube, and uses two of the
  268. nearest ones to the desired color for halftoning.
  269.  
  270. Types
  271. -----
  272.  
  273. Here is a brief explanation of the various types that appear as parameters
  274. or results of the drivers.
  275.  
  276. gx_color_value (defined in gxdevice.h)
  277.  
  278.     This is the type used to represent RGB or CMYK color values.  It is
  279. currently equivalent to unsigned short.  However, Ghostscript may use less
  280. than the full range of the type to represent color values:
  281. gx_color_value_bits is the number of bits actually used, and
  282. gx_max_color_value is the maximum value (equal to 2^gx_max_color_value_bits
  283. - 1).
  284.  
  285. gx_device (defined in gxdevice.h)
  286.  
  287.     This is the device structure, as explained above.
  288.  
  289. gs_matrix (defined in gsmatrix.h)
  290.  
  291.     This is a 2-D homogenous coordinate transformation matrix, used by
  292. many Ghostscript operators.
  293.  
  294. gx_color_index (defined in gxdevice.h)
  295.  
  296.     This is meant to be whatever the driver uses to represent a device
  297. color.  For example, it might be an index in a color map, or it might be
  298. R, G, and B values packed into a single integer.  Ghostscript doesn't ever
  299. do any computations with gx_color_index values: it gets them from
  300. map_rgb_color or map_cmyk_color and hands them back as arguments to
  301. several other procedures.  The special value gx_no_color_index (defined as
  302. (gx_color_index)(-1)) means "transparent" for some of the procedures.  The
  303. type definition is simply:
  304.  
  305.     typedef unsigned long gx_color_index;
  306.  
  307. gs_param_list (defined in gsparam.h)
  308.  
  309.     This is a parameter list, which is used to read and set attributes
  310. in a device.  See the comments in gsparam.h, and the description of the
  311. get_params and put_params procedures below, for more detail.
  312.  
  313. gx_tile_bitmap (defined in gxbitmap.h)
  314.  
  315.     This structure type represents a bitmap to be used as a tile for
  316. filling a region (rectangle).  Here is a copy of the relevant part of the
  317. file:
  318.  
  319. /*
  320.  * Structure for describing stored bitmaps.
  321.  * Bitmaps are stored bit-big-endian (i.e., the 2^7 bit of the first
  322.  * byte corresponds to x=0), as a sequence of bytes (i.e., you can't
  323.  * do word-oriented operations on them if you're on a little-endian
  324.  * platform like the Intel 80x86 or VAX).  Each scan line must start on
  325.  * a (32-bit) word boundary, and hence is padded to a word boundary,
  326.  * although this should rarely be of concern, since the raster and width
  327.  * are specified individually.  The first scan line corresponds to y=0
  328.  * in whatever coordinate system is relevant.
  329.  *
  330.  * For bitmaps used as halftone tiles, we may replicate the tile in
  331.  * X and/or Y, but it is still valuable to know the true tile dimensions.
  332.  */
  333. typedef struct gx_tile_bitmap_s {
  334.     byte *data;
  335.     int raster;            /* bytes per scan line */
  336.     gs_int_point size;        /* width, height */
  337.     gx_bitmap_id id;
  338.     ushort rep_width, rep_height;    /* true size of tile */
  339. } gx_tile_bitmap;
  340.  
  341. ********
  342. ******** Coding conventions ********
  343. ********
  344.  
  345. While most drivers (especially printer drivers) follow a very similar
  346. template, there is one important coding convention that is not obvious from
  347. reading the code for existing drivers: Driver procedures must not use
  348. malloc to allocate any storage that stays around after the procedure
  349. returns.  Instead, they must use gs_malloc and gs_free, which have slightly
  350. different calling conventions.  (The prototypes for these are in
  351. gsmemory.h, which is included in gx.h, which is included in gdevprn.h.)
  352. This is necessary so that Ghostscript can clean up all allocated memory
  353. before exiting, which is essential in environments that provide only
  354. single-address-space multi-tasking (specifically, Microsoft Windows).
  355.  
  356. char *gs_malloc(uint num_elements, uint element_size,
  357.   const char *client_name);
  358.  
  359.     Like calloc, but unlike malloc, gs_malloc takes an element count
  360. and an element size.  For structures, num_elements is 1 and element_size is
  361. sizeof the structure; for byte arrays, num_elements is the number of bytes
  362. and element_size is 1.  Unlike calloc, gs_malloc does NOT clear the block
  363. of storage.
  364.  
  365.     The client_name is used for tracing and debugging.  It must be a
  366. real string, not NULL.  Normally it is the name of the procedure in which
  367. the call occurs.
  368.  
  369. void gs_free(char *data, uint num_elements, uint element_size,
  370.   const char *client_name);
  371.  
  372.     Unlike free, gs_free demands that num_elements and element_size be
  373. supplied.  It also requires a client name, like gs_malloc.
  374.  
  375. All the driver procedures defined below that return int results return 0 on
  376. success, or an appropriate negative error code in the case of error
  377. conditions.  The error codes are defined in gserrors.h.  The relevant ones
  378. for drivers are as follows:
  379.  
  380.     gs_error_invalidfileaccess
  381.         An attempt to open a file failed.
  382.  
  383.     gs_error_limitcheck
  384.         An otherwise valid parameter value was too large for
  385.         the implementation.
  386.  
  387.     gs_error_rangecheck
  388.         A parameter was outside the valid range.
  389.  
  390.     gs_error_VMerror
  391.         An attempt to allocate memory failed.  (If this
  392.         happens, the procedure should release all memory it
  393.         allocated before it returns.)
  394.  
  395. If a driver does return an error, it should use the return_error
  396. macro rather than a simple return statement, e.g.,
  397.  
  398.     return_error(gs_error_VMerror);
  399.  
  400. This macro is defined in gx.h, which is automatically included by
  401. gdevprn.h but not by gserrors.h.
  402.  
  403. ********
  404. ******** Printer drivers ********
  405. ********
  406.  
  407. Printer drivers (which include drivers that write some kind of raster
  408. file) are especially simple to implement.  Of the driver procedures
  409. defined in the next section, they only need implement two: map_rgb_color
  410. (or map_cmyk_color) and map_color_rgb.  In addition, they must implement a
  411. print_page or print_page_copies procedure.  There are a set of macros in
  412. gdevprn.h that generate the device structure for such devices, of which
  413. the simplest is prn_device; for an example, see gdevbj10.c.  If you are
  414. writing a printer driver, we suggest you start by reading gdevprn.h and
  415. the subsection on "Color mapping" below; you may be able to ignore all the
  416. rest of the driver procedures.
  417.  
  418. The print_page procedures are defined as follows:
  419.  
  420. int (*print_page)(P2(gx_device_printer *, FILE *))
  421. int (*print_page_copies)(P3(gx_device_printer *, FILE *, int))
  422.  
  423.     This procedure must read out the rendered image from the device and
  424. write whatever is appropriate to the file.  To read back one or more scan
  425. lines of the image, the print_page procedure must call one of the following
  426. procedures:
  427.  
  428.     int gdev_prn_copy_scan_lines(P4(gx_device_printer *pdev, int y, byte *str,
  429.       uint size)
  430.  
  431. For this procedure, str is where the data should be copied to, and size is
  432. the size of the buffer starting at str.  This procedure returns the number
  433. of scan lines copied, or <0 for an error.  str need not be aligned.
  434.  
  435.     int gdev_prn_get_bits(gx_device_printer *pdev, int y, byte *str,
  436.       byte **actual_data)
  437.  
  438. This procedure reads out exactly one scan line.  If the scan line is
  439. available in the correct format already, *actual_data is set to point to it;
  440. otherwise, the scan line is copied to the buffer starting at str, and
  441. *actual_data is set to str.  This saves a copying step most of the time.
  442. str need not be aligned; however, if *actual_data is set to point to an
  443. existing scan line, it will be aligned.  (See the description of the
  444. get_bits procedure below for more details.)
  445.  
  446. In either case, each row of the image is stored in the form described
  447. in the comment under gx_tile_bitmap above; each pixel takes the
  448. number of bits specified as color_info.depth in the device structure,
  449. and holds values returned by the device's map_{rgb,cmyk}_color
  450. procedure.
  451.  
  452. The print_page procedure can determine the number of bytes required to hold
  453. a scan line by calling:
  454.  
  455.     uint gdev_prn_raster(P1(gx_device_printer *))
  456.  
  457. For a very simple concrete example, we suggest reading the code in
  458. bit_print_page in gdevbit.c.
  459.  
  460. If the device provides print_page, Ghostscript will call print_page the
  461. requisite number of times to print the desired number of copies; if the
  462. device provides print_page_copies, Ghostscript will call print_page_copies
  463. once per page, passing it the desired number of copies.
  464.  
  465. ********
  466. ******** Driver procedures ********
  467. ********
  468.  
  469. Most of the procedures that a driver may implement are optional.  If a
  470. device doesn't supply an optional procedure <proc>, the entry in the
  471. procedure structure may be either gx_default_<proc>, e.g.
  472. gx_default_tile_rectangle, or NULL or 0.  (The device procedure must also
  473. call the gx_default_ procedure if it doesn't implement the function for
  474. particular values of the arguments.)  Since C compilers supply 0 as the
  475. value for omitted structure elements, this convention means that
  476. statically initialized procedure structures will continue to work even if
  477. new (optional) members are added.
  478.  
  479. Life cycle
  480. ----------
  481.  
  482. A device instance start out life in a closed state.  In this state, no
  483. output operations will occur.  Only the following procedures may be called:
  484.     open_device
  485.     get_initial_matrix
  486.     get_params
  487.     put_params
  488.  
  489. When setdevice installs a device instance in the graphics state, it checks
  490. whether the instance is closed or open.  If the instance is closed,
  491. setdevice calls the open routine, and then sets the state to open.
  492.  
  493. There is currently no user-accessible operation to close a device instance.
  494. Device instances are only closed when they are about to be freed, which
  495. occurs in three situations:
  496.  
  497.     - When a 'restore' occurs, if the instance was created since the
  498. corresponding 'save';
  499.  
  500.     - By the garbage collector, if the instance is no longer accessible;
  501.  
  502.     - When Ghostscript exits (terminates).
  503.  
  504. Open/close/sync
  505. ---------------
  506.  
  507. int (*open_device)(P1(gx_device *)) [OPTIONAL]
  508.  
  509.     Open the device: do any initialization associated with making the
  510. device instance valid.  This must be done before any output to the device.
  511. The default implementation does nothing.
  512.  
  513. void (*get_initial_matrix)(P2(gx_device *, gs_matrix *)) [OPTIONAL]
  514.  
  515.     Construct the initial transformation matrix mapping user
  516. coordinates (nominally 1/72" per unit) to device coordinates.  The default
  517. procedure computes this from width, height, and x/y_pixels_per_inch on the
  518. assumption that the origin is in the upper left corner, i.e.
  519.         xx = x_pixels_per_inch/72, xy = 0,
  520.         yx = 0, yy = -y_pixels_per_inch/72,
  521.         tx = 0, ty = height.
  522.  
  523. int (*sync_output)(P1(gx_device *)) [OPTIONAL]
  524.  
  525.     Synchronize the device.  If any output to the device has been
  526. buffered, send / write it now.  Note that this may be called several times
  527. in the process of constructing a page, so printer drivers should NOT
  528. implement this by printing the page.  The default implementation does
  529. nothing.
  530.  
  531. int (*output_page)(P3(gx_device *, int num_copies, int flush)) [OPTIONAL]
  532.  
  533.     Output a fully composed page to the device.  The num_copies
  534. argument is the number of copies that should be produced for a hardcopy
  535. device.  (This may be ignored if the driver has some other way to specify
  536. the number of copies.)  The flush argument is true for showpage, false for
  537. copypage.  The default definition just calls sync_output.  Printer drivers
  538. should implement this by printing and ejecting the page.
  539.  
  540. int (*close_device)(P1(gx_device *)) [OPTIONAL]
  541.  
  542.     Close the device: release any associated resources.  After this,
  543. output to the device is no longer allowed.  The default implementation
  544. does nothing.
  545.  
  546. Color/alpha mapping
  547. -------------------
  548.  
  549. A given driver normally will implement either map_rgb_color or
  550. map_cmyk_color, but not both.  Black-and-white drivers do not need to
  551. implement either one.
  552.  
  553. gx_color_index (*map_rgb_color)(P4(gx_device *, gx_color_value red,
  554.   gx_color_value green, gx_color_value blue)) [OPTIONAL]
  555.  
  556.     Map a RGB color to a device color.  The range of legal values of
  557. the RGB arguments is 0 to gx_max_color_value.  The default algorithm uses
  558. the map_cmyk_color procedure if the driver supplies one, otherwise returns
  559. 1 if any of the values exceeds gx_max_color_value/2, 0 otherwise.
  560.  
  561.     Ghostscript assumes that for devices that have color capability
  562. (i.e., color_info.num_components > 1), map_rgb_color returns a color index
  563. for a gray level (as opposed to a non-gray color) iff red = green = blue.
  564.  
  565. gx_color_index (*map_cmyk_color)(P5(gx_device *, gx_color_value cyan,
  566.   gx_color_value magenta, gx_color_value yellow, gx_color_value black))
  567.   [OPTIONAL]
  568.  
  569.     Map a CMYK color to a device color.  The range of legal values of
  570. the CMYK arguments is 0 to gx_max_color_value.  The default algorithm
  571. calls the map_rgb_color procedure, with suitably transformed arguments.
  572.  
  573.     Ghostscript assumes that for devices that have color capability
  574. (i.e., color_info.num_components > 1), map_cmyk_color returns a color
  575. index for a gray level (as opposed to a non-gray color) iff cyan = magenta
  576. = yellow.
  577.  
  578. int (*map_color_rgb)(P3(gx_device *, gx_color_index color,
  579.   gx_color_value rgb[3])) [OPTIONAL]
  580.  
  581.     Map a device color code to RGB values.  The default algorithm
  582. returns (0 if color==0 else gx_max_color_value) for all three components.
  583.  
  584. gx_color_index (*map_rgb_alpha_color)(P5(gx_device *, gx_color_value red,
  585.   gx_color_value green, gx_color_value blue, gx_color_value alpha)) [OPTIONAL]
  586.  
  587.     Map a RGB color and an opacity value to a device color.  The range
  588. of legal values of the RGB and alpha arguments is 0 to gx_max_color_value;
  589. alpha = 0 means transparent, alpha = gx_max_color_value means fully
  590. opaque.  The default is to use the map_rgb_color procedure and ignore
  591. alpha.
  592.  
  593.     Note that if a driver implements map_rgb_alpha_color, it must also
  594. implement map_rgb_color, and must implement them in such a way that
  595. map_rgb_alpha_color(dev, r, g, b, gx_max_color_value) returns the same
  596. value as map_rgb_color(dev, r, g, b).
  597.  
  598.     Note that there is no map_cmyk_alpha_color procedure.  CMYK
  599. devices currently do not support variable opacity; alpha is ignored on
  600. such devices.
  601.  
  602. typedef enum { go_text, go_graphics } graphic_object_type;
  603. int (*get_alpha_bits)(P4(gx_device *dev, graphic_object_type type)) [OPTIONAL]
  604.  
  605.     Return the number of alpha (opacity) bits that should be used in
  606. rendering an object of the given type.  The default value is 1; the only
  607. values allowed are 1, 2, and 4.
  608.  
  609. Drawing
  610. -------
  611.  
  612. All drawing operations use device coordinates and device color values.
  613.  
  614. int (*fill_rectangle)(P6(gx_device *, int x, int y,
  615.   int width, int height, gx_color_index color))
  616.  
  617.     Fill a rectangle with a color.  The set of pixels filled is
  618. {(px,py) | x <= px < x + width and y <= py < y + height}.  In other words,
  619. the point (x,y) is included in the rectangle, as are (x+w-1,y), (x,y+h-1),
  620. and (x+w-1,y+h-1), but *not* (x+w,y), (x,y+h), or (x+w,y+h).  If width <=
  621. 0 or height <= 0, fill_rectangle should return 0 without drawing anything.
  622.  
  623. int (*draw_line)(P6(gx_device *, int x0, int y0, int x1, int y1,
  624.   gx_color_index color)) [OPTIONAL]
  625.  
  626.     Draw a minimum-thickness line from (x0,y0) to (x1,y1).  The
  627. precise set of points to be filled is defined as follows.  First, if y1 <
  628. y0, swap (x0,y0) and (x1,y1).  Then the line includes the point (x0,y0)
  629. but not the point (x1,y1).  If x0=x1 and y0=y1, draw_line should return 0
  630. without drawing anything.
  631.  
  632. Bitmap imaging
  633. --------------
  634.  
  635. Bitmap (or pixmap) images are stored in memory in a nearly standard way.
  636. The first byte corresponds to (0,0) in the image coordinate system: bits
  637. (or polybit color values) are packed into it left-to-right.  There may be
  638. padding at the end of each scan line: the distance from one scan line to
  639. the next is always passed as an explicit argument.
  640.  
  641. int (*copy_mono)(P11(gx_device *, const unsigned char *data, int data_x,
  642.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  643.   gx_color_index color0, gx_color_index color1))
  644.  
  645.     Copy a monochrome image (similar to the PostScript image
  646. operator).  Each scan line is raster bytes wide.  Copying begins at
  647. (data_x,0) and transfers a rectangle of the given width at height to the
  648. device at device coordinate (x,y).  (If the transfer should start at some
  649. non-zero y value in the data, the caller can adjust the data address by
  650. the appropriate multiple of the raster.)  The copying operation writes
  651. device color color0 at each 0-bit, and color1 at each 1-bit: if color0 or
  652. color1 is gx_no_color_index, the device pixel is unaffected if the image
  653. bit is 0 or 1 respectively.  If id is different from gx_no_bitmap_id, it
  654. identifies the bitmap contents unambiguously; a call with the same id will
  655. always have the same data, raster, and data contents.
  656.  
  657.     This operation, with color0 = gx_no_color_index, is the workhorse
  658. for text display in Ghostscript, so implementing it efficiently is very
  659. important.
  660.  
  661. int (*tile_rectangle)(P10(gx_device *, const gx_tile_bitmap *tile,
  662.   int x, int y, int width, int height,
  663.   gx_color_index color0, gx_color_index color1,
  664.   int phase_x, int phase_y)) [OPTIONAL]
  665.  
  666.     Tile a rectangle.  Tiling consists of doing multiple copy_mono
  667. operations to fill the rectangle with copies of the tile.  The tiles are
  668. aligned with the device coordinate system, to avoid "seams".
  669. Specifically, the (phase_x, phase_y) point of the tile is aligned with the
  670. origin of the device coordinate system.  (Note that this is backwards from
  671. the PostScript definition of halftone phase.)  phase_x and phase_y are
  672. guaranteed to be in the range [0..tile->width) and [0..tile->height)
  673. respectively.
  674.  
  675.     If color0 and color1 are both gx_no_color_index, then the tile is
  676. a color pixmap, not a bitmap: see the next section.
  677.  
  678.     This operation is the workhorse for halftone filling in
  679. Ghostscript, so implementing it efficiently for solid tiles (where either
  680. color0 and color1 are both gx_no_color_index, for colored halftones, or
  681. neither one is gx_no_color_index, for monochrome halftones) is very
  682. important.
  683.  
  684. Pixmap imaging
  685. --------------
  686.  
  687. Pixmaps are just like bitmaps, except that each pixel occupies more than
  688. one bit.  All the bits for each pixel are grouped together (this is
  689. sometimes called "chunky" or "Z" format).  For copy_color, the number of
  690. bits per pixel is given by the color_info.depth parameter in the device
  691. structure: the legal values are 1, 2, 4, 8, 16, 24, or 32.  The pixel
  692. values are device color codes (i.e., whatever it is that map_rgb_color
  693. returns).
  694.  
  695. int (*copy_color)(P9(gx_device *, const unsigned char *data, int data_x,
  696.   int raster, gx_bitmap_id id, int x, int y, int width, int height))
  697.  
  698.     Copy a color image with multiple bits per pixel.  The raster is in
  699. bytes, but x and width are in pixels, not bits.  If the device doesn't
  700. actually support color, this is OPTIONAL; the default is equivalent to
  701. copy_mono with color0 = 0 and color1 = 1.  If id is different from
  702. gx_no_bitmap_id, it identifies the bitmap contents unambiguously; a call
  703. with the same id will always have the same data, raster, and data
  704. contents.
  705.  
  706. We do not provide a separate procedure for tiling with a pixmap; instead,
  707. tile_rectangle can also take colored tiles.  This is indicated by the
  708. color0 and color1 arguments both being gx_no_color_index.  In this case,
  709. as for copy_color, the raster and height in the "bitmap" are interpreted
  710. as for real bitmaps, but the x and width are in pixels, not bits.
  711.  
  712. int (*copy_alpha)(P11(gx_device *dev, const unsigned char *data, int data_x,
  713.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  714.   gx_color_index color, int depth)) [OPTIONAL]
  715.  
  716.     Fill a given region with a given color modified by an individual
  717. alpha value for each pixel.  depth, the number of bits per pixel, is
  718. either 2 or 4, and in any case is always a value returned by a previous
  719. call on the get_alpha_bits procedure.  Note that if get_alpha_bits always
  720. returns 1, this procedure will never be called.
  721.  
  722. Bitmap/pixmap imaging
  723. ---------------------
  724.  
  725. int (*copy_rop)(P15(gx_device *dev,
  726.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  727.   const gx_color_index *scolors,
  728.   const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  729.   int x, int y, int width, int height,
  730.   int phase_x, int phase_y, int command)) [OPTIONAL]
  731.  
  732.     Combine an optional source image (as for copy_mono or copy_color)
  733. and an optional texture (a tile, as for tile_rectangle) with the existing
  734. bitmap or pixmap held by the driver, pixel by pixel, using any 3-input
  735. Boolean operation as modified by "transparency" flags.  This is a complex
  736. (and currently rather slow) operation.  The arguments are as follows:
  737.  
  738.     dev - the device, as for all driver procedures
  739.  
  740.     sdata, sourcex, sraster, id - as for copy_mono or copy_color
  741.       (data, data_x, raster, id)
  742.  
  743.     scolors - see below
  744.  
  745.     texture - same as the tile argument for tile_rectangle
  746.  
  747.     tcolors - see below
  748.  
  749.     x, y, width, height - as for the other copy/fill procedures
  750.  
  751.     phase_x, phase_y - as for tile_rectangle
  752.  
  753.     command - see below
  754.  
  755. The scolors argument is either NULL or a pointer to two gx_color_index
  756. values, depending on the nature of the source data:
  757.  
  758. Source        scolors[0]    scolors[1]
  759. ------        ----------    ----------
  760. solid color    the color    the color
  761. bitmap        color0/zero    color1/one    (as for copy_mono)
  762. pixmap        ---scolors is NULL---
  763.  
  764. Note that if the source is a bitmap with zero=0 and one=1, it can be treated
  765. as a pixmap (scolors=NULL).  Similarly, tcolors depends on the nature of the
  766. texture data:
  767.  
  768. Texture        tcolors[0]    tcolors[1]
  769. -------        ----------    ----------
  770. solid color    the color    the color
  771. bitmap        color0/zero    color1/one    (as for tile_rectangle)
  772. pixmap        ---tcolors is NULL---
  773.  
  774. Again, if the texture is a bitmap with color0=0 and color1=1, it can be
  775. treated as a pixmap (tcolors=NULL).
  776.  
  777. Command indicates the raster operation and transparency as follows:
  778.  
  779.     bits 7-0: raster op
  780.     bit 8: 0 if source opaque, 1 if source transparent
  781.     bit 9: 0 if texture opaque, 1 if texture transparent
  782.     bits ?-10: unused, must be 0
  783.  
  784. The raster operation follows the Microsoft and H-P specification.  It is an
  785. 8-element truth table that specifies the output value for each of the
  786. possible 2x2x2 input values as follows:
  787.  
  788.     Bit    Texture    Source    Destination
  789.     7    1    1    1
  790.     6    1    1    0
  791.     5    1    0    1
  792.     4    1    0    0
  793.     3    0    1    1
  794.     2    0    1    0
  795.     1    0    0    1
  796.     0    0    0    0
  797.  
  798. Transparency affects the output in the following way.  A source or texture
  799. pixel is considered transparent if its value is all 1's (e.g., 1 for
  800. bitmaps, 0xffffff for 24-bit RGB pixmaps) *and* the corresponding
  801. transparency bit is set in the command.  For each pixel, the result of the
  802. Boolean operation is written into the destination iff neither the source nor
  803. the texture pixel is transparent.  (Note that the H-P RasterOp
  804. specification, on which this is based, specifies that if the source and
  805. texture are both all 1's and the command specifies transparent source and
  806. opaque texture, the result *should* be written in the output.  We think this
  807. is an error in the documentation.)
  808.  
  809. Note that copy_rop is defined to operate on pixels in RGB space, again
  810. following the H-P and Microsoft specification.  For devices that don't use
  811. RGB (or gray-scale with black=0, white=all 1's) as their native color
  812. representation, the implementation of copy_rop must convert to RGB or gray
  813. space, do the operation, and convert back (or do the equivalent of this).
  814. The current default implementation does this only for black-and-white
  815. devices.
  816.  
  817. Here are the copy_rop equivalents of the most important previous imaging
  818. calls.  Note that rop3_S may be replaced by any other Boolean operation.
  819. For monobit devices, we assume that black = 1.  We assume the following
  820. declaration:
  821.     static const gx_color_index white2[2] = { 1, 1 };
  822.  
  823. (*fill_rectangle)(dev, x, y, w, h, color) ==>
  824.  
  825.     { gx_color_index colors[2];
  826.       colors[0] = colors[1] = color;
  827.       (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id, colors,
  828.                      NULL, colors /*irrelevant*/,
  829.                      x, y, w, h, 0, 0, rop3_S);
  830.     }
  831.  
  832. (*copy_mono)(dev, base, sourcex, sraster, id,
  833.          x, y, w, h, (gx_color_index)0, (gx_color_index)1) ==>
  834.  
  835.     (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  836.                    NULL, white2 /*irrelevant*/,
  837.                    x, y, w, h, 0, 0, rop3_S);
  838.  
  839. (*copy_color)(dev, base, sourcex, sraster, id,
  840.           x, y, w, h) ==> [same as copy_mono above]
  841.  
  842. (*copy_mono)(dev, base, sourcex, sraster, id,
  843.          x, y, w, h, gx_no_color_index, (gx_color_index)1) ==>
  844.  
  845.     (*dev_proc(dev, copy_rop))(dev, base, sourcex, sraster, id, NULL,
  846.                    NULL, white2 /*irrelevant*/,
  847.                    x, y, w, h, 0, 0,
  848.                    rop3_S | lop_S_transparent);
  849.  
  850. (*tile_rectangle)(dev, tile, x, y, w, h,
  851.           (gx_color_index)0, (gx_color_index)1, px, py) ==>
  852.  
  853.     (*dev_proc(dev, copy_rop))(dev, NULL, 0, 0, gx_no_bitmap_id,
  854.                    white2 /*irrelevant*/,
  855.                    tile, NULL,
  856.                    x, y, w, h, px, py, rop3_T)
  857.  
  858. Reading bits back
  859. -----------------
  860.  
  861. int (*get_bits)(P4(gx_device *, int y, byte *str, byte **actual_data))
  862.   [OPTIONAL]
  863.  
  864.     Read one scan line of bits back from the device into the area
  865. starting at str, namely, scan line y.  If the bits cannot be read back
  866. (e.g., from a printer), return -1; otherwise return 0.  The contents of the
  867. bits beyond the last valid bit in the scan line (as defined by the device
  868. width) are unpredictable.  str need not be aligned in any particular way.
  869.  
  870.     If actual_data is NULL, the bits are always returned at str.  If
  871. actual_data is not NULL, get_bits may either copy the bits to str and set
  872. *actual_data = str, or it may leave the bits where they are and return a
  873. pointer to them in *actual_data.  In the latter case, the bits are
  874. guaranteed to start on a 32-bit boundary and to be padded to a multiple of
  875. 32 bits; also in this case, the bits are not guaranteed to still be there
  876. after the next call on get_bits.
  877.  
  878. Parameters
  879. ----------
  880.  
  881. Devices may have an open-ended set of parameters, which are simply pairs
  882. consisting of a name and a value.  The value may be of various types:
  883. integer (int or long), boolean, float, string, name, null, array of integer,
  884. or array of float.  For example, the Name of a device is a string; the
  885. Margins of a device is an array of 2 floats.  See gsparam.h for more
  886. details.
  887.  
  888. If a device has parameters other than the ones applicable to all devices
  889. (or, in the case of printer devices, all printer devices), it must provide
  890. get_params and put_params procedures.  If your device has parameters beyond
  891. those of a straightforward display or printer, we strongly advise using the
  892. _get_params and _put_params procedures in an existing device (for example,
  893. gdevcdj.c or gdevbit.c) as a model for your own code.
  894.  
  895. int (*get_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  896.  
  897.     Read the parameters of the device into the parameter list at plist,
  898. using the param_write_* macros/procedures defined in gsparam.h.
  899.     
  900. int (*put_params)(P2(gx_device *dev, gs_param_list *plist)) [OPTIONAL]
  901.  
  902.     Set the parameters of the device from the parameter list at plist,
  903. using the param_read_* macros/procedures defined in gsparam.h.  All
  904. put_params procedures must use a "two-phase commit" algorithm; see gsparam.h
  905. for details.
  906.  
  907. External fonts
  908. --------------
  909.  
  910. Drivers may include the ability to display text.  More precisely, they may
  911. supply a set of procedures that in turn implement some font and text
  912. handling capabilities.  These procedures are documented in another file,
  913. xfonts.doc.  The link between the two is the driver procedure that
  914. supplies the font/text procedures:
  915.  
  916. xfont_procs *(*get_xfont_procs)(P1(gx_device *dev)) [OPTIONAL]
  917.  
  918.     Return a structure of procedures for handling external fonts and
  919. text display.  A NULL value means that this driver doesn't provide this
  920. capability.
  921.  
  922. For technical reasons, a second procedure is also needed:
  923.  
  924. gx_device *(*get_xfont_device)(P1(gx_device *dev)) [OPTIONAL]
  925.  
  926.     Return the device that implements get_xfont_procs in a non-default
  927. way for this device, if any.  Except for certain special internal devices,
  928. this is always the device argument.
  929.  
  930. Page devices
  931. ------------
  932.  
  933. gx_device *(*get_page_device)(P1(gx_device *dev)) [OPTIONAL]
  934.  
  935.     According to the Adobe specifications, some devices are "page
  936. devices" and some are not.  This procedure returns NULL if the device is
  937. not a page device, or the device itself if it is a page device.  In the
  938. case of forwarding devices, get_page_device returns the underlying page
  939. device (or NULL if the underlying device is not a page device).
  940.  
  941. Miscellaneous
  942. -------------
  943.  
  944. int (*get_band)(P3(gx_device *dev, int y, int *band_start)) [OPTIONAL]
  945.  
  946.     If the device is a band device, this procedure stores in *band_start
  947. the scan line (device Y coordinate) of the band that includes the given Y
  948. coordinate, and returns the number of scan lines in the band.  If the device
  949. is not a band device, this procedure returns 0.  The latter is the default
  950. implementation.
  951.